package com.habdqn.filght.information.web;

import com.alibaba.fastjson.JSON;
import com.habdqn.filght.information.entity.FlightTicket;
import com.habdqn.filght.information.service.FlightService;
import com.habdqn.filght.information.service.impl.FlightServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * @author Xing.Jiejian
 * @date 2020/11/24
 */
@WebServlet(name = "FindTicketsServlet",urlPatterns = "/tickets")
public class FindTicketsServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String strFlightId = request.getParameter("flightId");
        response.setContentType("application/json;charset=utf-8");
        PrintWriter out = response.getWriter();
        if (strFlightId!=null && !"".equals(strFlightId)){
            int flightId = Integer.parseInt(strFlightId);
            FlightService flightService = new FlightServiceImpl();
            List<FlightTicket> flightTicketList = flightService.findByFlightId(flightId);
            out.println(JSON.toJSONString(flightTicketList));
        }else{
            out.println("[]");
        }
    }
}
